/**
 * 
 */
package org.jopris.util;

import java.util.HashMap;
import java.util.Map;

/**
 * This class provide helper methods for resolving named classes into actual
 * <code>java.lang.Class</code> instances.
 * 
 * @author Guy Nir
 * @version 1.00
 */
public class ClassResolver {

	/**
	 * Maps between class type-name to actual <code>java.lang.Class</code>
	 * instance.
	 */
	private static final Map<String, Class<?>> primitiveTypes = getPrimitiveTypeMappings();

	/**
	 * @return A {@link Map} instance holding mappings between primitive types
	 *         and its associated <code>java.lang.Class</code> instances.
	 */
	private static Map<String, Class<?>> getPrimitiveTypeMappings() {
		Map<String, Class<?>> map = new HashMap<String, Class<?>>();
		map.put("boolean", boolean.class);
		map.put("byte", byte.class);
		map.put("char", char.class);
		map.put("short", short.class);
		map.put("int", int.class);
		map.put("long", long.class);
		map.put("float", float.class);
		map.put("double", double.class);
		map.put("void", void.class);

		return map;
	}

	/**
	 * Resolve a <i>className</i> into a real <code>java.lang.Class</code> type.
	 * This method extends the standard {@link Class#forName(String)} method by
	 * providing support for primitive types classes (e.g.: <code>boolean</code>
	 * , <code>int</code>, <code>float</code>, etc ...).
	 * 
	 * @param className
	 *            Name of class to look for.
	 * 
	 * @return Class denoted by <i>className</i>.
	 * 
	 * @throws ClassNotFoundException
	 *             If <i>className</i> is unknown (or not loaded yet).
	 */
	public static Class<?> getClassFor(String className) throws ClassNotFoundException {
		Class<?> clazz = primitiveTypes.get(className);
		if (clazz == null) {
			clazz = Class.forName(className, false, null);
		}
		return clazz;
	}
}
